import os
import shutil

import file_tools.file_utils as file_utils
from config.logger_config import init_logger
import stat

logger = init_logger()


class Dir:
    def __init__(self, description, path):
        self.description = description
        self.path = path


class CleanResult:
    def __init__(self, occupied, freed):
        self.occupied = occupied
        self.freed = freed


class WindowsCleaner:
    def __init__(self):
        self.user_path_prefix = 'C:/Users/'
        self.app_data_cache_dir = [
            Dir('pip缓存', 'AppData/Local/pip/cache'),
            Dir('vscode缓存, 你可以设置C_Cpp.intelliSenseCacheSize: 0禁用这个缓存',
                'AppData/Local/Microsoft/vscode-cpptools/ipch'),
            Dir('edge浏览器缓存', 'AppData/Local/Microsoft/Edge/User Data/Default/ExtensionActivityEdge'),
            Dir('网易云音乐缓存', 'AppData/Local/NetEase/CloudMusic/Cache'),
            Dir('阿里云盘缓存', '/AppData/Roaming/aDrive/Cache'),
            Dir('Windows下载的安装包', 'AppData/Local/Downloaded Installations'),
            Dir('Windows缓存', 'AppData/Local/Temp'),
            Dir('百度云缓存', 'Roaming/baidu/BaiduYunKernel/.accelerate'),
            Dir('WPS缓存', 'AppData/Roaming/kingsoft/office6/cache')
        ]
        self.absolute_cache_dir = [
            Dir('windows update cache', 'C:\\Windows\\SoftwareDistribution\\Download')
        ]
        pass

    def get_users(self):
        users = []
        # listdir会把隐藏文件和受系统保护的文件都显示出来
        for username in os.listdir(self.user_path_prefix):
            user_home = f'{self.user_path_prefix}/{username}'
            if os.path.isdir(user_home):
                users.append(username)
        return users

    def delete_item(self, scan_path, description):
        if os.path.exists(scan_path) and not file_utils.is_folder_empty(scan_path):
            try:
                occupied = file_utils.get_size(scan_path)
                freed = file_utils.clear_path(scan_path)
                logger.info(
                    f'清理 {description}, 占用: {file_utils.format_size(occupied)}, 删除: {file_utils.format_size(freed)}, 路径 {scan_path}')
                return CleanResult(occupied, freed)
            except Exception as e:
                logger.error(f'无法删除文件, 错误信息: {e}', e)
                return CleanResult(0, 0)
        else:
            return CleanResult(0, 0)
            # logger.info(f'skip {scan_path}, path is not exists')

    def scan_cache(self):
        users = self.get_users()
        user_name = os.getlogin()
        total_freed = 0
        total_occupied = 0
        logger.info(f'你好, {user_name}')
        for username in users:
            logger.info(f'正在扫描用户 {username} 的 AppData...')
            for cache_dir in self.app_data_cache_dir:
                result = self.delete_item(f'{self.user_path_prefix}/{username}/{cache_dir.path}', cache_dir.description)
                total_freed += result.freed
                total_occupied += result.occupied
        for cache_dir in self.absolute_cache_dir:
            result = self.delete_item(cache_dir.path, cache_dir.description)
            total_freed += result.freed
            total_occupied += result.occupied
        logger.info(
            f'全部完成. 扫描到{file_utils.format_size(total_occupied)} 总共释放 {file_utils.format_size(total_freed)}')


if __name__ == "__main__":
    cleaner = WindowsCleaner()
    cleaner.scan_cache()
